package com.hetang;

import com.alibaba.druid.pool.DruidDataSource;
import com.hetang.controller.filter.BaseInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.sql.DataSource;

/**
 * @author 詹泽
 * @date 2017/3/3
 */
@Configuration
@EnableTransactionManagement
public class Config extends WebMvcConfigurerAdapter{

    @Autowired
    BaseInterceptor baseInterceptor;

    @Autowired
    private Environment env;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/addUser").setViewName("views/add");
        registry.addViewController("/findUser").setViewName("views/find");
        registry.addViewController("/updateUser").setViewName("views/update");
        super.addViewControllers(registry);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //设置请求响应头
        registry.addInterceptor(baseInterceptor).addPathPatterns("/**");
    }

    //使用druid数据源
    @Bean
    public DataSource writeDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }

}
